package com.itbaizhan.shopping_manager_api.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.Admin;
import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.AdminService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

/*
* 后台管理员相关
* */
@RestController
@RequestMapping("/admin")
public class AdminController {

     @DubboReference
    private AdminService adminService;
    @Autowired
    private PasswordEncoder passwordEncoder;
/*
* 新增管理员
* */
     @PostMapping("/add")
    public BaseResult add(@RequestBody  Admin admin)
     {
         admin.setPassword(passwordEncoder.encode(admin.getPassword()));
         adminService.add(admin);
         return BaseResult.ok();
     }

    /*
     * 修改管理员
     * */
     @PutMapping("/update")
    public BaseResult update(@RequestBody  Admin admin)
     {
         String password = admin.getPassword();
         if (StringUtils.hasText(password)){ // 密码不为空加密
             password = passwordEncoder.encode(password);
             admin.setPassword(password);
         }
         adminService.update(admin);
         return BaseResult.ok();
     }
/*
*
* 删除管理员
* */
     @DeleteMapping("/delete")
    public BaseResult delete(Long aid)
     {
         adminService.delete(aid);
         return BaseResult.ok();
     }


     /*
     * 根据id查询管理员
     * */
     @GetMapping("/findById")
    public BaseResult findById(Long aid)
     {
         Admin admin = adminService.findById(aid);
         return BaseResult.ok(admin);
     }


     /*
     * 分页查询管理员
     *
     * */
    @GetMapping("/search")
//    @PreAuthorize("hasAnyAuthority('/admin/search')")
    public BaseResult<Page<Admin>> search(int page, int size)
    {
        Page<Admin> search = adminService.search(page, size);
        return BaseResult.ok(search);
    }


    /**
     * 修改管理员角色
     * @param aid
     * @param rids
     * @return
     */
    @PutMapping("/updateRoleToAdmin")
    public BaseResult updateRoleToAdmin(Long aid,Long[] rids)
    {
        adminService.updateRoleToAdmin(aid,rids);
        return BaseResult.ok();
    }

    /**
     *
     * @return 管理员名
     */
    @GetMapping("/getUsername")
    public BaseResult<String> getUsername(){
        //1.获取会话对象
        SecurityContext context = SecurityContextHolder.getContext();
        //2.获取认证对象
        Authentication authentication = context.getAuthentication();
        //3.获取认证对象中的用户信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String username = userDetails.getUsername();
        return BaseResult.ok(username);
    }
}
